import { AuditEntMgtParam } from '../enterpriseManagement/model/enterpriseManagementModel';
import {
  AppItem,
  EtpAppTableItem,
  EtpAppTableParams,
  FreezeParam,
  RenewParam,
  SourceItem,
  StatusItem,
  TenantItem,
} from './model/enterpriseAppManagementModel';
import { formatToDateTime } from '/@/utils/dateUtil';
import { tenantHttp } from '/@/utils/http/axios';
import { deployHttp } from '/@/utils/http/axios';

enum Api {
  GET_ENTERPRISE_APP_TABLE = '/tenant/app/page',
  GET_STATUS = '/tenant/app/status',
  GET_TENANTS = '/tenant/app/tenants',
  GET_APPS = '/tenant/app/apps',
  GET_SOURCES = '/tenant/app/source',
  AUDIT = '/tenant/app/audit',
  RENEW = '/v1/resource/renew',
  FREEZE = '/v1/resource/freeze',
  UNFREEZE = '/v1/resource/unfreeze',
}

export const getEnterpriseMgtTable = async (params: EtpAppTableParams) => {
  const res = await tenantHttp.post<EtpAppTableItem>({
    url: Api.GET_ENTERPRISE_APP_TABLE,
    params,
  });
  res.items.map((item) => {
    if (item.status == 'underReview') {
      item.statusDesc = '审核中';
    }
    if (item.status == 'rejected') {
      item.statusDesc = '审核未通过';
    }
    if (item.status == 'deploying') {
      item.statusDesc = '部署中';
    }
    if (item.status == 'normal') {
      item.statusDesc = '正常(使用中)';
    }
    if (item.status == 'freeze') {
      item.statusDesc = '冻结';
    }
    if (item.status == 'release') {
      item.statusDesc = '释放';
    }
    if (item.status == 'fail') {
      item.statusDesc = '部署失败';
    }
    if (formatToDateTime(item.effectiveTime) !== 'Invalid date') {
      item.effectiveTime = formatToDateTime(item.effectiveTime);
    }
    if (formatToDateTime(item.expireTime) !== 'Invalid date') {
      item.expireTime = formatToDateTime(item.expireTime);
    }
    if (item.effectiveTime === null) {
      item.effectiveTime = '--';
    }
    if (item.source === null) {
      item.source = '--';
    }
    return item;
  });
  return res;
};

export const getAppStatusOpt = async () => {
  const res = await tenantHttp.get<StatusItem[]>({
    url: Api.GET_STATUS,
  });
  const result = res.map((item) => {
    return {
      label: item.desc,
      value: item.status,
    };
  });
  return result;
};

export const getTenantOpt = async () => {
  const res = await tenantHttp.get<TenantItem[]>({
    url: Api.GET_TENANTS,
  });
  const result = res.map((item) => {
    return {
      label: item.tenantName,
      value: item.tenantId,
    };
  });
  return result;
};

export const getAppOpt = async () => {
  const res = await tenantHttp.get<AppItem[]>({
    url: Api.GET_APPS,
  });
  const result = res.map((item) => {
    return {
      label: item.appName,
      value: item.appId,
    };
  });
  return result;
};

export const getSourceOpt = async () => {
  const res = await tenantHttp.get<SourceItem[]>({
    url: Api.GET_SOURCES,
  });
  const result = res.map((item) => {
    return {
      label: item.desc,
      value: item.source,
    };
  });
  return result;
};

export const auditEntApp = async (params: AuditEntMgtParam) => {
  const res = await tenantHttp.post<string>({
    url: Api.AUDIT,
    params,
  });
  return res;
};

export const renewApp = async (params: RenewParam) => {
  const res = await deployHttp.post<string>({
    url: Api.RENEW,
    params,
  });
  return res;
};

export const freezeApp = async (params: FreezeParam) => {
  const res = await deployHttp.post<string>({
    url: Api.FREEZE,
    params,
  });
  return res;
};

export const unfreezeApp = async (params: FreezeParam) => {
  const res = await deployHttp.post<string>({
    url: Api.UNFREEZE,
    params,
  });
  return res;
};
